package com.canbot.u05.adapter.dance

import android.content.Context
import android.support.v4.view.MotionEventCompat
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.canbot.u05.R
import com.canbot.u05.bean.dance.ActionBean
import kotlinx.android.synthetic.main.item_dance_action.view.*

class DanceActionAdapter(internal val mContext: Context, private var mbrowList: List<ActionBean>) : RecyclerView.Adapter<DanceActionAdapter.MyViewHolder>() {

        private var index: Int = 0
        private var itemOnClickListener: ItemOnClickListener? = null
        private var mDragStartListener: OnStartDragListener? = null
        inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
                var dance_name: TextView = view.dance_name
                var dance_duration: TextView = view.dance_duration

        }
        fun setData(actionLists:List<ActionBean>){
                this.mbrowList = actionLists
                notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_dance_action, parent, false)
                return MyViewHolder(view)
        }
        fun addItemOnClickListener(itemOnClickListener: ItemOnClickListener){
                this.itemOnClickListener = itemOnClickListener
        }
        override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
                val actionBean = mbrowList.get(position)
                holder.dance_name.text = actionBean.name
                holder.dance_duration.text = "${actionBean.duration}s"
                holder.itemView.setOnClickListener {
                        itemOnClickListener?.onClick(position)
                }

                holder.itemView.setOnTouchListener(object :View.OnTouchListener{
                        override fun onTouch(v: View, event: MotionEvent): Boolean {
                                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                                        mDragStartListener?.onStartDrag(holder);
                                }
                                return false
                        }

                })
        }
        interface OnStartDragListener{
                fun onStartDrag(holder:RecyclerView.ViewHolder)
        }
        fun setOnDragStartListener(mDragStartListener: OnStartDragListener) {
                this.mDragStartListener = mDragStartListener
        }
        override fun getItemId(i: Int): Long {
                return i.toLong()
        }

        override fun getItemCount(): Int {
                return mbrowList.size
        }

        fun setIndex(i: Int) {
                index = i
        }

        interface ItemOnClickListener{
                fun onClick(index:Int)
        }

}
